package com.rs;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.rs.game.World;
import com.rs.game.item.Item;
import com.rs.game.player.Player;
import com.rs.utils.Encrypt;
import com.rs.utils.SerializableFilesManager;
import com.rs.utils.Utils;

public class Panel extends JFrame implements ListSelectionListener {

	private static final long serialVersionUID = 4033776481784053894L;
	
	private JPanel contentPane;
	private JLabel lblPlayersOnline;
	private JTextField txtPlayerName;
	private JList<String> list;
	private DefaultListModel<String> listModel;
	private JTextField txtNewUsername;
	private JTextField txtNewPassword;
	
	private Player player;

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Panel frame = new Panel();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public static void start() {
		try {
			UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
			Panel frame = new Panel();
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Panel() {
		setTitle("Styx - Control Panel");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 451);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		lblPlayersOnline = new JLabel("Players Online: " + World.getPlayers().size());
		lblPlayersOnline.setHorizontalAlignment(SwingConstants.CENTER);
		lblPlayersOnline.setBounds(10, 11, 114, 23);
		contentPane.add(lblPlayersOnline);
		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				refreshPlayers();
			}
		});
		btnRefresh.setBounds(10, 375, 114, 23);
		contentPane.add(btnRefresh);
		
		txtPlayerName = new JTextField();
		txtPlayerName.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (SerializableFilesManager.containsPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()))) {
					txtPlayerName.setText(Utils.formatPlayerNameForDisplay(txtPlayerName.getText()));
					return;
				}
				sendNotification("Invalid Player Name", "The name \"" + txtPlayerName.getText() + "\" isn't valid.", true);
				txtPlayerName.setText("");
			}
		});
		txtPlayerName.setBounds(215, 39, 114, 23);
		contentPane.add(txtPlayerName);
		
		JLabel lblPlayerName = new JLabel("Player Name:");
		lblPlayerName.setHorizontalAlignment(SwingConstants.CENTER);
		lblPlayerName.setLabelFor(txtPlayerName);
		lblPlayerName.setBounds(215, 11, 114, 23);
		contentPane.add(lblPlayerName);
		
		listModel = new DefaultListModel<String>();
		list = new JList<String>(listModel);
		list.addListSelectionListener(this);
		list.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		list.setBounds(10, 39, 114, 324);
		contentPane.add(list);
		
		JLabel lblNewUsername = new JLabel("Change Username:");
		lblNewUsername.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewUsername.setBounds(134, 70, 114, 23);
		contentPane.add(lblNewUsername);
		
		txtNewUsername = new JTextField();
		txtNewUsername.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				changeUsername();
			}
		});
		lblNewUsername.setLabelFor(txtNewUsername);
		txtNewUsername.setBounds(134, 98, 114, 23);
		contentPane.add(txtNewUsername);
		txtNewUsername.setColumns(10);
		
		JButton btnGiveExtreme = new JButton("Give Extreme");
		btnGiveExtreme.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				makeExtreme();
			}
		});
		btnGiveExtreme.setBounds(295, 304, 114, 23);
		contentPane.add(btnGiveExtreme);
		
		JButton btnGiveSuper = new JButton("Give Super");
		btnGiveSuper.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				makeSuper();
			}
		});
		btnGiveSuper.setBounds(295, 270, 114, 23);
		contentPane.add(btnGiveSuper);
		
		JButton btnGiveDonor = new JButton("Give Donor");
		btnGiveDonor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				makeDonor();
			}
		});
		btnGiveDonor.setBounds(295, 236, 114, 23);
		contentPane.add(btnGiveDonor);
		
		JButton btnGiveMod = new JButton("Give Moderator");
		btnGiveMod.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				promote(false, true);
			}
		});
		btnGiveMod.setBounds(295, 202, 114, 23);
		contentPane.add(btnGiveMod);
		
		JButton btnGiveSupport = new JButton("Give Support");
		btnGiveSupport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				promote(true, false);
			}
		});
		btnGiveSupport.setBounds(295, 168, 114, 23);
		contentPane.add(btnGiveSupport);
		
		JButton btnDemote = new JButton("Demote Player");
		btnDemote.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				demote(true);
			}
		});
		btnDemote.setBounds(295, 134, 114, 23);
		contentPane.add(btnDemote);
		
		JButton btnResetStats = new JButton("Reset Stats");
		btnResetStats.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset(true, false, false);
			}
		});
		btnResetStats.setBounds(134, 132, 114, 23);
		contentPane.add(btnResetStats);
		
		JButton btnResetItems = new JButton("Reset Items");
		btnResetItems.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset(false, true, false);
			}
		});
		btnResetItems.setBounds(134, 168, 114, 23);
		contentPane.add(btnResetItems);
		
		JButton btnResetPoints = new JButton("Reset Points");
		btnResetPoints.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset(false, false, true);
			}
		});
		btnResetPoints.setBounds(134, 202, 114, 23);
		contentPane.add(btnResetPoints);
		
		JButton btnMute = new JButton("Mute");
		btnMute.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mutePlayer();
			}
		});
		btnMute.setBounds(134, 236, 114, 23);
		contentPane.add(btnMute);
		
		JButton btnTemporaryBan = new JButton("Temporary Ban");
		btnTemporaryBan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				banPlayer(true);
			}
		});
		btnTemporaryBan.setBounds(134, 270, 114, 23);
		contentPane.add(btnTemporaryBan);
		
		JButton btnPermanentBan = new JButton("Permanent Ban");
		btnPermanentBan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				banPlayer(false);
			}
		});
		btnPermanentBan.setBounds(134, 304, 114, 23);
		contentPane.add(btnPermanentBan);
		
		JLabel lblNewPassword = new JLabel("Change Password:");
		lblNewPassword.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewPassword.setBounds(295, 70, 114, 23);
		contentPane.add(lblNewPassword);
		
		txtNewPassword = new JTextField();
		txtNewPassword.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				changePassword();
			}
		});
		lblNewPassword.setLabelFor(txtNewPassword);
		txtNewPassword.setColumns(10);
		txtNewPassword.setBounds(295, 99, 114, 23);
		contentPane.add(txtNewPassword);
		
		JButton btnUpdateServer = new JButton("Update Server");
		btnUpdateServer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				update();
			}
		});
		btnUpdateServer.setBounds(134, 375, 275, 23);
		contentPane.add(btnUpdateServer);
		
		JButton btnResolve = new JButton("Resolve");
		btnResolve.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resolveOffences();
			}
		});
		btnResolve.setBounds(134, 339, 114, 23);
		contentPane.add(btnResolve);
		
		JButton btnTakeDonation = new JButton("Take Donation");
		btnTakeDonation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				takeDonations();
			}
		});
		btnTakeDonation.setBounds(295, 339, 114, 23);
		contentPane.add(btnTakeDonation);
	}
	
	public void refreshPlayers() {
		listModel.clear();
		for (Player p : World.getPlayers()) {
			listModel.addElement(p.getDisplayName());
		}
		lblPlayersOnline.setText("Players Online: " + World.getPlayers().size());
	}
	
	private void update() {
		int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to restart the server?", "Confirm Update", JOptionPane.YES_NO_OPTION);
		if (option == JOptionPane.YES_OPTION) {
			int delay = 300;
			World.safeShutdown(true, delay);
		}
	}
	
	private void reset(boolean stats, boolean items, boolean points) {
		player = World.getPlayerByDisplayName(txtPlayerName.getText());
		if (player == null) {
			player = SerializableFilesManager.loadPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			if (player != null) {
				player.setUsername(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			}
		}
		if (player == null) {
			sendNotification("Invalid Player Name", "Please enter a valid player name before trying this.", true);
			return;
		}
		if (stats) {
			int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to reset this player's stats?", "Confirm Reset", JOptionPane.YES_NO_OPTION);
			if (option == JOptionPane.YES_OPTION) {
				if (World.containsPlayer(txtPlayerName.getText())) {
					player.getSession().getChannel().close();
				}
				//player.getSkills().;
				player.getSkills().init();
				SerializableFilesManager.savePlayer(player);
			}
		}
		if (items) {
			int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to reset this player's items?", "Confirm Reset", JOptionPane.YES_NO_OPTION);
			if (option == JOptionPane.YES_OPTION) {
				if (World.containsPlayer(txtPlayerName.getText())) {
					player.getSession().getChannel().close();
				}
				for (int i = 0; i < Utils.getItemDefinitionsSize(); i++) {
					player.getEquipment().getItems().removeAll(new Item(i, Integer.MAX_VALUE));
					player.getInventory().getItems().removeAll(new Item(i, Integer.MAX_VALUE));
					player.getBank().removeItem(i);
					int[] BankSlot = player.getBank().getItemSlot(i);
					player.getBank().removeItem(BankSlot, Integer.MAX_VALUE, false, true);
				}
				//player.getMoneyPouch().
				SerializableFilesManager.savePlayer(player);
			}
		}
		if (points) {
			int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to reset this player's points?", "Confirm Reset", JOptionPane.YES_NO_OPTION);
			if (option == JOptionPane.YES_OPTION) {
				if (World.containsPlayer(txtPlayerName.getText())) {
					player.getSession().getChannel().close();
				}
				//player.setPkPoints(0);
				player.setDungtokens(0);
				player.setPestPoints(0);
				SerializableFilesManager.savePlayer(player);
			}
		}
	}
	
	private void changePassword() {
		player = World.getPlayerByDisplayName(txtPlayerName.getText());
		if (player == null) {
			player = SerializableFilesManager.loadPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			if (player != null) {
				player.setUsername(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			}
		}
		if (player == null) {
			sendNotification("Invalid Player Name", "Please enter a valid player name before trying this.", true);
			return;
		}
		if (!player.getUsername().equalsIgnoreCase("archangel") && !player.getUsername().equalsIgnoreCase("alex")) {
			int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to change this player's password?", "Confirm Change", JOptionPane.YES_NO_OPTION);
			if (option == JOptionPane.YES_OPTION) {
				if (txtNewPassword.getText().length() < 3) {
					sendNotification("Incorrect Password Length", "You must enter a password containing no less than 3 characters.", true);
					return;
				}
				if (txtNewPassword.getText().length() > 15) {
					sendNotification("Incorrect Password Length", "You must enter a password containing no more than 15 characters.", true);
					return;
				}
				player.setPassword(Encrypt.encryptSHA1(txtNewPassword.getText()));
				SerializableFilesManager.savePlayer(player);
				sendNotification("Success", txtPlayerName.getText() + "'s password is now \"" + txtNewPassword.getText() + "\".", false);
				txtNewPassword.setText("");
				return;
			}
		}
		sendNotification("Error", "You cannot change this player's password.", true);
	}
	
	private void changeUsername() {
		player = World.getPlayerByDisplayName(txtPlayerName.getText());
		if (player == null) {
			player = SerializableFilesManager.loadPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			if (player != null) {
				player.setUsername(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			}
		}
		if (player == null) {
			sendNotification("Invalid Player Name", "Please enter a valid player name before trying this.", true);
			return;
		}
		if (!player.getUsername().equalsIgnoreCase("archangel") && !player.getUsername().equalsIgnoreCase("alex")) {
			int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to change this player's username?", "Confirm Change", JOptionPane.YES_NO_OPTION);
			if (option == JOptionPane.YES_OPTION) {
				if (txtNewUsername.getText().length() < 2) {
					sendNotification("Incorrect Username Length", "You must enter a username containing no less than 2 characters.", true);
					return;
				}
				if (txtNewUsername.getText().length() > 12) {
					sendNotification("Incorrect Username Length", "You must enter a username containing no more than 12 characters.", true);
					return;
				}
				player.setUsername(Utils.formatPlayerNameForProtocol(txtNewUsername.getText()));
				SerializableFilesManager.savePlayer(player);
				sendNotification("Success", txtPlayerName.getText() + "'s username is now \"" + txtNewUsername.getText() + "\".", false);
				return;
			}
		}
		sendNotification("Error", "You cannot change this player's username.", true);
	}
	
	private void mutePlayer() {
		player = World.getPlayerByDisplayName(txtPlayerName.getText());
		if (player == null) {
			player = SerializableFilesManager.loadPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			if (player != null) {
				player.setUsername(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			}
		}
		if (player == null) {
			sendNotification("Invalid Player Name", "Please enter a valid player name before trying this.", true);
			return;
		}
		if (!player.getUsername().equalsIgnoreCase("archangel") && !player.getUsername().equalsIgnoreCase("alex")) {
			int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to temporarily mute this player?", "Confirm Punishment", JOptionPane.YES_NO_OPTION);
			if (option == JOptionPane.YES_OPTION) {
				player.setMuted(Utils.currentTimeMillis() + (48 * 60 * 60 * 1000));
				SerializableFilesManager.savePlayer(player);
				sendNotification("Success", "You've successfully temporarily muted \"" + txtPlayerName.getText() + "\" for 48 hours.", false);
				return;
			}
		}
		sendNotification("Error", "You cannot punish this player.", true);
	}
	
	private void banPlayer(boolean temporary) {
		player = World.getPlayerByDisplayName(txtPlayerName.getText());
		if (player == null) {
			player = SerializableFilesManager.loadPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			if (player != null) {
				player.setUsername(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			}
		}
		if (player == null) {
			sendNotification("Invalid Player Name", "Please enter a valid player name before trying this.", true);
			return;
		}
		if (!player.getUsername().equalsIgnoreCase("archangel") && !player.getUsername().equalsIgnoreCase("alex")) {
			if (temporary) {
				int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to temporarily ban this player?", "Confirm Punishment", JOptionPane.YES_NO_OPTION);
				if (option == JOptionPane.YES_OPTION) {
					player.setBanned(Utils.currentTimeMillis() + (48 * 60 * 60 * 1000));
					if (World.containsPlayer(txtPlayerName.getText())) {
						player.getSession().getChannel().close();
					}
					SerializableFilesManager.savePlayer(player);
					sendNotification("Success", "You've successfully temporarily banned \"" + txtPlayerName.getText() + "\" for 48 hours.", false);
					return;
				}
			} else {
				int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to permanently ban this player?", "Confirm Punishment", JOptionPane.YES_NO_OPTION);
				if (option == JOptionPane.YES_OPTION) {
					player.setPermBanned(true);
					if (World.containsPlayer(txtPlayerName.getText())) {
						player.getSession().getChannel().close();
					}
					SerializableFilesManager.savePlayer(player);
					sendNotification("Success", "You've successfully permanently banned \"" + txtPlayerName.getText() + "\".", false);
					return;
				}
			}
		}
		sendNotification("Error", "You cannot punish this player.", true);
	}
	
	private void resolveOffences() {
		player = World.getPlayerByDisplayName(txtPlayerName.getText());
		if (player == null) {
			player = SerializableFilesManager.loadPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			if (player != null) {
				player.setUsername(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			}
		}
		if (player == null) {
			sendNotification("Invalid Player Name", "Please enter a valid player name before trying this.", true);
			return;
		}
		if (player.isPermBanned()) {
			player.setPermBanned(false);
		}
		if (player.getBanned() > 0) {
			player.setBanned(0);
		}
		if (player.getMuted() < 0) {
			player.setMuted(0);
		}
		SerializableFilesManager.savePlayer(player);
		sendNotification("Success", "You've successfully removed all punishments from \"" + txtPlayerName.getText() + "\".", false);
	}
	
	private void takeDonations() {
		player = World.getPlayerByDisplayName(txtPlayerName.getText());
		if (player == null) {
			player = SerializableFilesManager.loadPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			if (player != null) {
				player.setUsername(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			}
		}
		if (player == null) {
			sendNotification("Invalid Player Name", "Please enter a valid player name before trying this.", true);
			return;
		}
		if (player.isSupremeDonator()) {
			player.setSupremeDonator(false);
		}
		if (player.isExtremeDonator()) {
			player.setExtremeDonator(false);
		}
		if (player.isDonator()) {
			player.setDonator(false);
		}
		SerializableFilesManager.savePlayer(player);
		sendNotification("Success", "You've successfully taken all donations from \"" + txtPlayerName.getText() + "\".", false);
	}
	
	private void makeDonor() {
		player = World.getPlayerByDisplayName(txtPlayerName.getText());
		if (player == null) {
			player = SerializableFilesManager.loadPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			if (player != null) {
				player.setUsername(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			}
		}
		if (player == null) {
			sendNotification("Invalid Player Name", "Please enter a valid player name before trying this.", true);
			return;
		}
		player.setDonator(true);
		player.setExtremeDonator(false);
		player.setSupremeDonator(false);
		SerializableFilesManager.savePlayer(player);
		sendNotification("Success", "You've successfully given \"" + txtPlayerName.getText() + "\" Donator status.", false);
	}
	
	private void makeSuper() {
		player = World.getPlayerByDisplayName(txtPlayerName.getText());
		if (player == null) {
			player = SerializableFilesManager.loadPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			if (player != null) {
				player.setUsername(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			}
		}
		if (player == null) {
			sendNotification("Invalid Player Name", "Please enter a valid player name before trying this.", true);
			return;
		}
		player.setDonator(false);
		player.setExtremeDonator(true);
		player.setSupremeDonator(false);
		SerializableFilesManager.savePlayer(player);
		sendNotification("Success", "You've successfully given \"" + txtPlayerName.getText() + "\" Super Donator status.", false);
	}
	
	private void makeExtreme() {
		player = World.getPlayerByDisplayName(txtPlayerName.getText());
		if (player == null) {
			player = SerializableFilesManager.loadPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			if (player != null) {
				player.setUsername(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			}
		}
		if (player == null) {
			sendNotification("Invalid Player Name", "Please enter a valid player name before trying this.", true);
			return;
		}
		player.setDonator(false);
		player.setExtremeDonator(false);
		player.setSupremeDonator(true);
		SerializableFilesManager.savePlayer(player);
		sendNotification("Success", "You've successfully given \"" + txtPlayerName.getText() + "\" Extreme Donator status.", false);
	}
	
	private void promote(boolean support, boolean moderator) {
		player = World.getPlayerByDisplayName(txtPlayerName.getText());
		if (player == null) {
			player = SerializableFilesManager.loadPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			if (player != null) {
				player.setUsername(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			}
		}
		if (player == null) {
			sendNotification("Invalid Player Name", "Please enter a valid player name before trying this.", true);
			return;
		}
		if (!player.getUsername().equalsIgnoreCase("archangel") && !player.getUsername().equalsIgnoreCase("alex")) {
			if (player.getRights() > 1) {
				sendNotification("Improper Usage", "Please use the demote button to demote players.", true);
				return;
			}
			if (support) {
				if (!player.isSupporter()) {
					if (player.getRights() == 0) {
						player.setSupporter(true);
						SerializableFilesManager.savePlayer(player);
						sendNotification("Success", "You've successfully promoted \"" + txtPlayerName.getText() + "\" to Player Support.", false);
						World.sendWorldMessage(txtPlayerName.getText() + " has been promoted to <img=11><col=58ACFA><shad=2E2EFE>Player Support</shad></col>.", false);
						return;
					}
					sendNotification("Improper Usage", "Please use the demote button to demote players.", true);
					return;
				}
				sendNotification("Error", "This player is already on the Player Support team.", true);
				return;
			}
			if (moderator) {
				if (player.getRights() != 1) {
					if (player.isSupporter()) {
						player.setSupporter(false);
					}
					player.setRights(1);
					SerializableFilesManager.savePlayer(player);
					sendNotification("Success", "You've successfully promoted \"" + txtPlayerName.getText() + "\" to Moderator.", false);
					World.sendWorldMessage(txtPlayerName.getText() + " has been promoted to <img=0><col=FFFFFF<shad=000000Moderator</shad></col>.", false);
					return;
				}
				sendNotification("Error", "This player is already a moderator.", true);
				return;
			}
		}
		sendNotification("Error", "You cannot alter this player's rank.", true);
	}
	
	private void demote(boolean all) {
		player = World.getPlayerByDisplayName(txtPlayerName.getText());
		if (player == null) {
			player = SerializableFilesManager.loadPlayer(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			if (player != null) {
				player.setUsername(Utils.formatPlayerNameForProtocol(txtPlayerName.getText()));
			}
		}
		if (player == null) {
			sendNotification("Invalid Player Name", "Please enter a valid player name before trying this.", true);
			return;
		}
		int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to demote this player?", "Confirm Punishment", JOptionPane.YES_NO_OPTION);
		if (option == JOptionPane.YES_OPTION) {
			if (!player.getUsername().equalsIgnoreCase("archangel") && !player.getUsername().equalsIgnoreCase("alex")) {
				if (all) {
					if (player.isSupporter()) {
						player.setSupporter(false);
					}
					player.setRights(0);
					World.sendWorldMessage(txtPlayerName.getText() + " has been demoted.", false);
					SerializableFilesManager.savePlayer(player);
					sendNotification("Success", "You've successfully demoted \"" + txtPlayerName.getText() + "\".", false);
					return;
				}
				int previousRights = player.getRights();
				if (previousRights == 2) {
					player.setRights(1);
					World.sendWorldMessage(txtPlayerName.getText() + " has been demoted to <img=0><col=FFFFFF><shad=000000>Moderator</shad></col>.", false);
					SerializableFilesManager.savePlayer(player);
					sendNotification("Success", "You've successfully demoted \"" + txtPlayerName.getText() + "\".", false);
					return;
				}
				if (previousRights == 1) {
					player.setRights(0);
					World.sendWorldMessage(txtPlayerName.getText() + " has been demoted to <img=11><col=58ACFA><shad=2E2EFE>Player Support</shad></col>.", false);
					player.setSupporter(true);
					SerializableFilesManager.savePlayer(player);
					sendNotification("Success", "You've successfully demoted \"" + txtPlayerName.getText() + "\".", false);
					return;
				}
				if (player.isSupporter()) {
					player.setRights(0);
					player.setSupporter(false);
					SerializableFilesManager.savePlayer(player);
					sendNotification("Success", "You've successfully demoted \"" + txtPlayerName.getText() + "\".", false);
					return;
				}
			}
			sendNotification("Error", "You cannot demote this player.", true);
		}
	}
	
	private void sendNotification(String title, String message, boolean error) {
		JOptionPane.showMessageDialog(null, message, title, (error ? JOptionPane.ERROR_MESSAGE : JOptionPane.INFORMATION_MESSAGE));
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
        if (e.getValueIsAdjusting()) {
        	if (list.getSelectedIndex() == -1) {
        		//do nothing
        	} else {
        		txtPlayerName.setText(Utils.formatPlayerNameForDisplay(list.getSelectedValue().toString()));
        		list.clearSelection();
        	}
        }
	}
}